<?php

namespace app\test\controller;

use think\Controller;
use think\Db;
use think\exception\ErrorException;
use think\Request;

class Question extends Controller
{
    // 按照搜索进行查询
    public function search(Request $request) {
        $course = $request->param('course');
        $page = $request->param('page');
        $search = Db::name('question')->where('course',$course)->where('page',$page)->select();
        return json($search);
    }

    // 查询所有
    public function searchAll() {
        $data = Db::table('question')->select();
        return json($data);
    }

    public function add(Request $request) {
        $data = [
            'course' => $request->param('course'),
            'page' => $request->param('page'),
            'question' => $request->param('question'),
            'answer' => $request->param('answer')
        ];
        $add = Db::name('question')->insert($data);
        return json($add);
    }

    public function delete(Request $request) {
        $id = $request->param('id');
        $delete = Db::name('question')->where('id',$id)->delete();
        return json($delete);
    }

    public function edit(Request $request) {
        // 根据id进行查找并进行修改数据
        $id = $request->param('id');
        $data = [
            'course' => $request->param('course'),
            'page'  => $request->param('page'),
            'question' => $request->param('question'),
            'answer' => $request->param('answer')
        ];
        $edit = Db::name('question')->where('id',$id)->update($data);
        return json($edit);
    }
}
